﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Security.Cryptography;

namespace dHash
{
    public delegate string hashfunc(string word,string salt);
    
    


    public abstract class clientCallBack : MarshalByRefObject
    {
        public void ping(string message)
        {
            Console.WriteLine("Message from server: {0}", message);
        }
        public abstract void addjob(jobdetails job);
        public abstract void canceljob(int taskhash);
  
    }

    public interface dashServer
    {
        void connect(clientCallBack ccb);
        void found(int taskhash, string password);
        void failed(int taskhash, int jobid);

    }


    public class hash
    {
        static MD5 md5 = MD5CryptoServiceProvider.Create();
        static Encoding enc = Encoding.GetEncoding(0);

        public static hashfunc getfunc(hashfuncs func)
        {
            switch (func)
            {
                case hashfuncs.md5_md5pwd_salt:
                    return new hashfunc(hash.md5_md5pwd_salt);

                case hashfuncs.md5_pdw_salt:
                    return new hashfunc(hash.md5pwd_salt);
                default:
                    return null;

            }

        }

        static string md5hash(string input)
        {
            byte[] raw = enc.GetBytes(input);
            byte[] result = md5.ComputeHash(raw);

            string hash = "";

            foreach (byte b in result)
            {
                hash += b.ToString("x2");
            }

            return hash;
        }

        public static string md5pwd_salt(string word, string salt)
        {
            return md5hash(word + salt);
        }

        public static string md5_md5pwd_salt(string word, string salt)
        {
            return md5hash(md5hash(word) + salt);
        }



    }

    [Serializable]
    public struct jobdetails
    {
        public string prefix;
        public string salt;
        public int taskhash;
        public int lentogo;
        public int jobid;
        public string charset;
        public string targethash;
        public hashfuncs func;
    }

    public enum hashfuncs
    {
        md5_md5pwd_salt,
        md5_pdw_salt,
    }
}
